package GDoc::Job::Fetch::US::Exec::En::OE::Misc;

use Moose;

use GDoc::Job;
use File::Temp;

extends 'GDoc::Job::Fetch';

sub setup { 
    my $self = shift;
    $self->_baseurl( 'http://www.oe.energy.gov/information_center/documents.htm' );
    $self->_basedir( 'http://www.oe.energy.gov' );
}

sub run { 
    my $self = shift;

    if ( $self->data->{link} ) { 
        $self->_fetch_pdf;
    } else { 
        $self->logger->info( "fetching base " . $self->_baseurl );
        my $mech = $self->_mech;
        $mech->get( $self->_baseurl );

        my @links = $mech->find_all_links( url_regex => qr{\.pdf$} );
        my $cnt = 1000;     # arbitrary nums
        foreach my $link( @links ) {
            my $title = $link->text;
            my $job = GDoc::Job->new( class => 'Fetch::US::Exec::En::OE::Misc',
                                      queue => $self->fetch_queue,
                                      data  => { link  => $link->url,
                                                 desc  => $title,
                                                 num   => ++$cnt
                                               }
                                    );

            $job->submit;
        }
    }
}


sub _fetch_pdf { 
    my $self = shift;

    my %data = %{ $self->data };

    my $ident = sprintf( 'OE-MISC-%04d', $data{num} );
    my $filename = $ident . '.pdf';
    my $key = 'us/exec/en/oe/' . $filename;

    $self->logger->info( "Storing $data{link} --> $key" );

    my $mech = $self->_mech;
    my $tmpf = File::Temp->new;

    $mech->get( $self->_basedir . $data{link}, ':content_file' => $tmpf->filename );

    $self->store_pdf( key      => $key,
                      datafile => $tmpf,
                      filename => $filename,
                      meta     => { description    => $data{desc},
                                    identifier     => $ident,
                                    arbitrary_nums => 1
                                  }
                    );

}



1;
